package consul

import (
	"common/config"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"go.uber.org/zap"
)

type NewConsulConfig struct {
	Host string
	Port int
}

func NewClient(host string, port int) *NewConsulConfig {
	return &NewConsulConfig{
		Host: host,
		Port: port,
	}
}

func (NewConsulConfig) Register(name string, tags []string, port int, address string) {
	registration := api.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    name,
		Tags:    tags,
		Port:    port,
		Address: address,
	}
	file := `Service=` + name
	filter, _ := config.Consul.Agent().ServicesWithFilter(file)
	if len(filter) != 0 {
		zap.S().Error("注册失败，已经注册了")
		return
	} else {
		err := config.Consul.Agent().ServiceRegister(&registration)
		if err != nil {
			//zap.S().Error("注册失败", err.Error())
			return
		} else {
			zap.S().Info("注册成功")
		}
	}

}
